//
//  ViewController.m
//  UIPageControl+UIScrollView
//
//  Created by 王恕 on 16/1/21.
//  Copyright © 2016年 王恕. All rights reserved.
//

#import "ViewController.h"
#import "PageController.h"
@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UIPageControl *myPageControl;
@property (nonatomic, strong) NSArray *contentList;
@property (nonatomic, strong) NSArray *coverList;
@property (nonatomic, strong) NSMutableArray *ViewControllers;


@end

@implementation ViewController

- (UIScrollView *)myScrollView{

    if (!_myScrollView) {
        
        //创建UIScrollView对象
        _myScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        //设置背景色
        _myScrollView.backgroundColor = [UIColor grayColor];
        _myScrollView.pagingEnabled = YES;
        //设置UIScrollView的contentSize－－就是它可滚动的区域大小
        _myScrollView.contentSize = CGSizeMake(_myScrollView.frame.size.width * _contentList.count, _myScrollView.frame.size.height);
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _myScrollView.showsVerticalScrollIndicator = NO;
        _myScrollView.scrollsToTop = NO;
        //设置该控制器作为UIScrollView的委托对象
        _myScrollView.delegate = self;
        [self.view addSubview:_myScrollView];

    }
    return _myScrollView;
}

- (UIPageControl *) myPageControl{

    if (!_myPageControl) {
        //创建UIPageControl控件
        _myPageControl = [[UIPageControl alloc] init];
        _myPageControl.frame = CGRectMake(0, _myScrollView.frame.size.height - 80, _myScrollView.frame.size.width, 80);
        _myPageControl.pageIndicatorTintColor = [UIColor blueColor];
        _myPageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _myPageControl.currentPage = 0;
        _myPageControl.numberOfPages = _contentList.count;
      
        [self.view addSubview:_myPageControl];

    }
    return _myPageControl;
}



- (void) change{
    if (self.myPageControl.currentPage < 3) {
         self.myPageControl.currentPage = self.myPageControl.currentPage +1;
    }
    else{
    self.myPageControl.currentPage = 0;

    }
    [self changePage:self.myPageControl.currentPage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentList = @[@"启动页",@"首页",@"开发入门",@"最佳实践"];
    self.coverList = @[@"1",@"2",@"3",@"4"];
    /*
     程序采用懒加载的方式来创建PageController控制器
     因此此处只向数组中添加一些null作为占位符
     等到程序需要时才真正创建PageController
     */
    self.ViewControllers = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < self.contentList.count; i++) {
        [self.ViewControllers addObject:[NSNull null]];
    }

    [self myScrollView];
    [self myPageControl];
    //每隔1秒掉用一次change方法
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
    
}

- (void) changePage:(NSInteger)page{

 
    CGRect bounds = self.myScrollView.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * page;
    bounds.origin.y = 0;
    
    [self.myScrollView scrollRectToVisible:bounds animated:YES];
    
    
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
}

- (void) loadScrollViewWithPage: (NSUInteger)page{
    if (page >= self.contentList.count)
        return;
        PageController *controller = self.ViewControllers[page];
        
        if ((NSNull *)controller == [NSNull null]) {
            controller = [[PageController alloc] initwithPageNumber:page];
            [self.ViewControllers replaceObjectAtIndex:page withObject:controller];
        }
        
        if (controller.view.superview == nil) {
            CGRect frame = self.myScrollView.frame;
            frame.origin.x = CGRectGetWidth(frame) * page;
            frame.origin.y = 0;
            
            controller.view.frame = frame;
            controller.imageLabel.text = self.contentList[page];
            controller.myImage.image = [UIImage imageNamed:self.coverList[page]];
            
            [self addChildViewController:controller];
            [self.myScrollView addSubview:controller.view];
        }
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
